package com.liuheqiao.sushegaunli.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.liuheqiao.sushegaunli.R;
import com.liuheqiao.sushegaunli.dao.DormitoryDao;
import com.liuheqiao.sushegaunli.dao.UserDao;
import com.liuheqiao.sushegaunli.db.DBHelper;
import com.liuheqiao.sushegaunli.model.Dormitory;
import com.liuheqiao.sushegaunli.model.User;

public class MineFragment extends Fragment {
    private OnMineItemClickListener listener;
    private TextView tvRealName;
    private TextView tvRoomNo;
    private TextView tvDormInfo;
    private ImageView ivAvatar;

    // 定义接口
    public interface OnMineItemClickListener {
        void onUserInfoClick();
        void onMyApplicationsClick();
        void onSettingsClick();
        void onHelpClick();
        void onAboutClick();
    }

    public void setOnMineItemClickListener(OnMineItemClickListener listener) {
        this.listener = listener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        initViews(view);
        setupClickListeners(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadUserInfo();
    }

    private void initViews(View view) {
        tvRealName = view.findViewById(R.id.tv_real_name);
        tvRoomNo = view.findViewById(R.id.tv_room_no);
        tvDormInfo = view.findViewById(R.id.tv_dorm_info);
        ivAvatar = view.findViewById(R.id.iv_avatar);
    }

    private void setupClickListeners(View view) {
        // 用户信息卡片点击
        view.findViewById(R.id.cv_user_info).setOnClickListener(v -> {
            if (listener != null) {
                listener.onUserInfoClick();
            }
        });

        // 我的申请点击
        view.findViewById(R.id.ll_my_applications).setOnClickListener(v -> {
            if (listener != null) {
                listener.onMyApplicationsClick();
            }
        });

        // 设置点击
        view.findViewById(R.id.ll_settings).setOnClickListener(v -> {
            if (listener != null) {
                listener.onSettingsClick();
            }
        });

        // 帮助点击
        view.findViewById(R.id.ll_help).setOnClickListener(v -> {
            if (listener != null) {
                listener.onHelpClick();
            }
        });

        // 关于我们点击
        view.findViewById(R.id.ll_about).setOnClickListener(v -> {
            if (listener != null) {
                listener.onAboutClick();
            }
        });
    }

    private void loadUserInfo() {
        UserDao userDao = new UserDao(new DBHelper(requireContext()));
        User currentUser = userDao.getCurrentUser(requireContext());
        
        if (currentUser != null) {
            // 设置用户姓名
            if (currentUser.getRealName() != null) {
                tvRealName.setText(currentUser.getRealName());
            }
            
            // 获取宿舍信息
            String buildingNo = currentUser.getBuildingNo();
            String roomNo = currentUser.getRoomNo();
            
            if ("-1".equals(buildingNo) || "-1".equals(roomNo)) {
                // 未分配宿舍
                tvRoomNo.setText("未分配");
                tvDormInfo.setVisibility(View.GONE);  // 隐藏入住信息
            } else {
                // 已分配宿舍
                DormitoryDao dormitoryDao = new DormitoryDao(new DBHelper(requireContext()));
                Dormitory dormitory = dormitoryDao.getDormitoryByNumber(buildingNo, roomNo);
                
                if (dormitory != null) {
                    // 设置宿舍号
                    String roomInfo = String.format("%s栋%s", 
                            dormitory.getBuildingNo(), 
                            dormitory.getRoomNo());
                    tvRoomNo.setText(roomInfo);

                    // 设置宿舍详细信息
                    tvDormInfo.setVisibility(View.VISIBLE);  // 显示入住信息
                    String dormInfo = String.format("当前入住：%d/%d人", 
                            dormitory.getCurrentCount(),
                            dormitory.getCapacity());
                    tvDormInfo.setText(dormInfo);
                }
            }

            // TODO: 设置用户头像
            // ivAvatar.setImageResource(R.drawable.default_avatar);
        }
    }
} 